// pages/se-publish/se-publish.js
const app=getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        src:[]
    },
    // 判断并提交表单数据
    formSubmit:function (e){
        var that = this
        console.log(e.detail.value);
        if(e.detail.value.petName.length == 0){
            wx.showToast({
                title:'姓名不能为空',
                icon:'loading',
                duration:1500
            })
            // setTimeout(function(){
            //     wx.hideToast()
            // },2000)
        }else if(e.detail.value.petAge.length == 0){
            wx.showToast({
                title:'年龄不能为空',
                icon:'loading',
                duration:1500
            })
        } else if(e.detail.value.petType.length == 0){
            wx.showToast({
                title:'品种不能为空',
                icon:'loading',
                duration:1500
            })
        } else if(e.detail.value.yimiao.length == 0){
            wx.showToast({
                title:'打没打疫苗啊',
                icon:'loading',
                duration:1500
            })
        } else if(e.detail.value.time2.length == 0){
            wx.showToast({
                title:'时间不能为空',
                icon:'loading',
                duration:1500
            })
        } else if(e.detail.value.phone.length == 0 || e.detail.value.phone.length != 11){
            wx.showToast({
                title:'联系方式有误',
                icon:'loading',
                duration:1500
            })
        } else if(e.detail.value.area.length == 0){
            wx.showToast({
                title:'位置不能为空',
                icon:'loading',
                duration:1500
            })
        }else{
            wx.request({
                url:`${app.globalData.url}/adopt/adopt`,
                header:{
                    "Content-Type":"application/x-www-form-urlencoded"
                },
                method:"POST",
                data:{
                    Pid:wx.getStorageSync('Pid'),
                    Uid:wx.getStorageSync('Uid'),
                    Pname:e.detail.value.petName,
                    Page:e.detail.value.petAge,
                    Pvarie:e.detail.value.petType,
                    Pinject:e.detail.value.yimiao,
                    Ftime:e.detail.value.time2,
                    Uphone:e.detail.value.phone,
                    Uarea:e.detail.value.area,
                    Pintroduction:e.detail.value.introd,
                    Photo:that.data.src
                },
                success:function(res){
                    console.log(res.data);
                    wx.showToast({
                        title:'正在上传',
                        icon:'loading',
                        duration:1000
                    })
                    setTimeout(function(){
                        wx.navigateTo({
                            url:"/pages/add1/add1"
                        })
                    },1500)
                },
                fail:function(res){
                    console.log('出错了');
                }
            })
        }
        // this.setData({
        //     name:e.detail.value
        // })
    },

    //添加图片
    chooseImage:function(){
        var that = this;
          wx.chooseImage({
            count: 12,
            sizeType:['original','compressed'],
            sourceType:['album','camera'],
            success(res){
              // console.log(res.tempFilePaths)
              const tempFilePaths = res.tempFilePaths
              that.setData({
                src:[...that.data.src,...res.tempFilePaths]
              })
            },
            fail(res){
              console.log('添加图片失败！')
            }
          })
      },
      //预览图片
      previewImage:function(e){
          var that = this
          var src = e.currentTarget.dataset.src
          wx.previewImage({
              current: src,
              urls:that.data.src
          })
      },
      //删除图片
      deleteImage:function(e){
        var that = this;
        var src = that.data.src
        var index = e.currentTarget.dataset.index  //获取当前长按图片下标
        wx.showModal({
          title:'提示',
          content:'确定要删除此图片吗？',
          success: function(res){
            if(res.confirm){
              src.splice(index,1);
              console.log('点击确定了')
            }else if(res.cancel){
              console.log('点击取消了')
              return false;
            }
            that.setData({
              src
            })
          }
        })
      },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})